/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdoit.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import justdoit.model.Task;
import justdoit.model.User;

/**
 * Everything related with Task and User
 * @author Can
 */
public class TaskManager {

    private static User currentUser;
    private static XMLManager xmlManager = new XMLManager();

    // return the instance of current user logined
    public static User getCurrentUser() {
        return currentUser;
    }

    // return a list of pending tasks under the current user
    public static List<Task> getPendingTasks() {
        List<Task> allTasks = currentUser.getTasks();
        List<Task> pendingTasks = new ArrayList<Task>();
        for (Task t : allTasks) {
            if (t.getStatus() == 0) {
                try {
                    Date dueDate = DateFormat.getDateInstance(DateFormat.SHORT).parse(t.getDeadLine());
                    //Date dueDate = t.getDueDate().getTime();
                    System.out.println(dueDate.toString());
                    if (!dueDate.before(new Date())) {
                        pendingTasks.add(t);
                    }
                } catch (ParseException ex) {
                    System.out.println(ex.getLocalizedMessage());
                    pendingTasks.add(t);    // important!
                }
            }
        }
        return pendingTasks;
    }

    // return a list of completed tasks under the current user
    public static List<Task> getCompletedTasks() {
        List<Task> allTasks = currentUser.getTasks();
        List<Task> completedTasks = new ArrayList<Task>();
        for (Task t : allTasks) {
            if (t.getStatus() == 1) {
                completedTasks.add(t);
            }
        }
        return completedTasks;
    }

    // return a list of overdue tasks under the current user
    public static List<Task> getOverdueTasks() {
        List<Task> allTasks = currentUser.getTasks();
        List<Task> overdueTasks = new ArrayList<Task>();
        for (Task t : allTasks) {
            if (t.getStatus() == 0) {
                try {
                    Date dueDate = DateFormat.getDateInstance(DateFormat.SHORT).parse(t.getDeadLine());
                    System.out.println(dueDate.toString());
                    if (dueDate.before(new Date())) {
                        overdueTasks.add(t);
                    }
                } catch (ParseException ex) {
                    System.out.println(ex.getLocalizedMessage());
                }
            }
        }
        return overdueTasks;
    }

    // get a list of all user names exist in database
    public static String[] getUserNameList() {
        return xmlManager.getUserNameList();
    }

    // return a object of User or null if not found
    public static User getUserByUserName(String userName) {
        currentUser = xmlManager.getUserByUserName(userName);
        return currentUser;
    }

    public static int addUser(User newUser) {
        if (xmlManager.addUser(newUser) == 0) {
            currentUser = newUser;
            return 0;
        }
        return -1;
    }

    public static User createDefaultUser(Locale locale) {
        User newUser = new User(LanguageManager.getString("initialUserName"),
                locale);
        if (addUser(newUser) == -1) {
            newUser = null;
        }
        return newUser;
    }

    public static int updateUser(User userToUpdate) {
        if (xmlManager.updateUser(userToUpdate) == 0) {
            currentUser = userToUpdate;
            return 0;
        }
        return -1;
    }

    public static int deleteUser() {
        if (xmlManager.deleteUser(currentUser) == 0) {
            currentUser = null;
            return 0;
        }
        return -1;
    }

    public static int addTask(Task newTask) {
        if (xmlManager.addTask(newTask, currentUser.getId()) == 0) {
            currentUser.getTasks().add(newTask);
            return 0;
        }
        return -1;
    }

    public static int deleteTask(Task taskToDelete) {
        if (xmlManager.deleteTask(taskToDelete, currentUser.getId()) == 0) {
            if (currentUser.getTasks().remove(taskToDelete)) {
                return 0;
            }
        }
        return -1;
    }

    public static int updateTask(Task taskToUpdate) {
        if (xmlManager.updateTask(taskToUpdate, currentUser.getId()) == 0) {
            int index = currentUser.getTasks().indexOf(taskToUpdate);
            currentUser.getTasks().toArray()[index] = taskToUpdate;
            return 0;
        }
        return -1;
    }

    public static int login(String userName, char[] password) {
        User user = getUserByUserName(userName);
        if (user == null) {
            return -1;  // user not found
        }
        if (user.matchPassword(password)) {
            currentUser = user;
            return 0;   //success
        }
        return -2;  //password not match
    }

    public static int markAsDone(Task task) {
        if (task == null) {
            return -1;
        }
        task.setStatus(1);
        return updateTask(task);
    }

    public static List<String> getAllCategories() {
        return xmlManager.getAllCategories(currentUser.getUserName());
    }
}
